import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Enumeration;

@SuppressWarnings("serial")
public class GameModeGUI extends JPanel implements ActionListener {
	
	static String onePlayer = "1 Player Test Mode";
    static String multiPlayer = "Multiplayer";
	static String[] playerCount = { "2", "3", "4", "5", "6", "7", "8" };
	
	static JFrame frame;
	static JRadioButton onePlayerButton;
	static JRadioButton multiPlayerButton;
	static JComboBox playerCountList;
	static ButtonGroup group;
	static JButton okayButton;
	static JButton cancelButton;
	static JPanel radioPanel;
	static JPanel buttonPanel;
	
	public GameModeGUI() {
	
		super(new BorderLayout());
		
		frame = new JFrame("Game Mode");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
		//Create
		onePlayerButton = new JRadioButton(onePlayer);
        onePlayerButton.setMnemonic(KeyEvent.VK_B);
        onePlayerButton.setActionCommand(onePlayer);
        onePlayerButton.setSelected(true);
		
		multiPlayerButton = new JRadioButton(multiPlayer);
        multiPlayerButton.setMnemonic(KeyEvent.VK_B);
        multiPlayerButton.setActionCommand(multiPlayer);
		
		playerCountList = new JComboBox(playerCount);
		playerCountList.setSelectedIndex(0);
		playerCountList.addActionListener(this);
		playerCountList.setEnabled(false);
		
		okayButton = new JButton("Okay");
		okayButton.setMnemonic(KeyEvent.VK_M);
		okayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okayButtonActionPerformed(evt);
            }
        });
		
		cancelButton = new JButton("Cancel");
		cancelButton.setMnemonic(KeyEvent.VK_M);
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
		
		//Group
		group = new ButtonGroup();
        group.add(onePlayerButton);
        group.add(multiPlayerButton);
		
		//Add action listener
		onePlayerButton.addActionListener(this);
        multiPlayerButton.addActionListener(this);
		playerCountList.addActionListener(this);
		
		//Align in a column
		radioPanel = new JPanel(new GridLayout(0, 1, 2, 1));
        radioPanel.add(onePlayerButton);
        radioPanel.add(multiPlayerButton);
		radioPanel.add(playerCountList);
		
		buttonPanel = new JPanel(new GridLayout(0, 2, 2, 2));
		buttonPanel.add(okayButton);
		buttonPanel.add(cancelButton);
		
		frame.getContentPane().add(radioPanel, BorderLayout.PAGE_START);
		frame.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
		
        setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
		
		frame.pack(); 
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
        
		if (multiPlayerButton.isSelected() == true) {
		
			playerCountList.setEnabled(true);
		}
		
		if (onePlayerButton.isSelected() == true) {
		
			playerCountList.setEnabled(false);
		}
    }
	
	public JRadioButton getSelected(ButtonGroup group) {
	
		for (Enumeration E = group.getElements(); E.hasMoreElements();) {
			
			JRadioButton b = (JRadioButton)E.nextElement();
        
			if (b.getModel() == group.getSelection()) {
				
				return b;
			}
		}
		
		return null;
	}

	
	private void okayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
		JRadioButton b = this.getSelected(group);
		//ChooseTokenGUI ctGUI;
		
		if (b == onePlayerButton) {
			
			new ChooseTokenGUI(1).setVisible(true);
		}
		
		else {
			
			new ChooseTokenGUI(Integer.parseInt((String)playerCountList.getSelectedItem())).setVisible(true);
		}
		
		frame.setVisible(false);
    }
	
	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
		System.exit(0);
    }
}